<?php
require_once ('DataBaseQuery.php');
if ((!isset($_POST["myusername1"])) || (!isset($_POST["mypassword1"])))
    header('Location: welcome.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>AdCo Editor | Collaborative Editor</title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="language" content="en" />
        <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="header"><div id="header2">
                <div id="logo">
                    <h1><a href="#">AdCo Editor</a></h1>
                    <p id="subtitle">Online Editor</p>
                </div>
                <div id="menu">
                    <ul>
                        <li><a href="welcome.php">Home</a></li>
                        <li><a href="#">About</a></li>
                        <li><a href="#">Services</a></li>
                        <li><a href="#">Gallery</a></li>
                        <li><a href="#">Help</a></li>
                        <li><a href="#">Contact</a></li>
                    </ul>
                </div>
            </div></div><!-- header --><!-- header2 -->
        <div id="main"><div id="main2">
                <div id="sidebar">
                    <h2>Login</h2>
                    <?php
                    $login = false;
                    if ($_POST["myusername1"] == null)
                        echo "<font color = " . '"red"' . ">" . "Warning: Please complete the " . '"username" field' . "</font>";
                    if ($_POST["mypassword1"] == null)
                        echo "<br>" . "<font color = " . '"red"' . ">" . "Warning: Please complete the " . '"password" field' . "</font>";
                    if (($_POST["myusername1"] == null) || ($_POST["mypassword1"] == null))
                        echo "<br><input type=button onClick=" . '"parent.location=' . "'welcome.php'" . '"' . " value=" . '"Back"><br><br>';
                    else {
                        $pass = getUserPassword($_POST["myusername1"]);
                        if ($pass == null) {
                            echo "<b><font color = " . '"red">' . "Invalid username</font></b>";
                            echo "<br><input type=button onClick=" . '"parent.location=' . "'welcome.php'" . '"' . " value=" . '"Back"><br><br>';
                        } else {
                            if (strcmp($pass, $_POST["mypassword1"]) != 0) {
                                echo "<b><font color = " . '"red">' . "Invalid password</font></b>";
                                echo "<br><input type=button onClick=" . '"parent.location=' . "'welcome.php'" . '"' . " value=" . '"Back"><br><br>';
                            } else {
                                echo "<b><font color = " . '"green">' . "Welcome " . $_POST["myusername1"] . "</font></b>";
                                echo "<br><input type=button onClick=" . '"parent.location=' . "'welcome.php'" . '"' . " value=" . '"Logout"><br><br>';
                                $login = true;
                            }
                        }
                    }
                    ?>
                </div><!-- sidebar -->
                <div id="content">
                    <div class="post">
                        <h2><a href="#">Personal Files</a></h2>
                        <?php
                        if ($login) {
                            $pf = listPersonalFiles($_POST["myusername1"]);
                            foreach($pf as $f)
                            {
                                $fname = getFileName($f);
                                $file_id = getFileId2($f);
                                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Files<br><br>";
                                echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . "<a href = " . '"file_edit.php?uid=' . $_POST["myusername1"] . '&id_f=' . $f . '">' . $fname . "</a>&nbsp;&nbsp;";
                                echo "<input type=button onClick=" . '"parent.location=' . "'settings.php?uid=" . $_POST["myusername1"] . "&id_f=" . $f . "'" . '"' . " value=" . '"Edit Permisions"><br><br>';
                            }
                        }
                        ?>
                    </div>
                    <div class="post">
                        <h2><a href="#">Accessible Files</a></h2>
                        <p class="postmeta">Updated |
                            <?php
                            $date = getdate();
                            echo $date["weekday"] . ", " . $date["month"] . " " . $date["mday"] . ", " . $date["year"];
                            ?>
                        </p>
                        <div class="entry">
                            <?php
                            if ($login) {
                                $u = getUserId($_POST["myusername1"]);
                                $vf = listVisibleFiles($u);
                                foreach($vf as $f)
                                {
                                    $user_id = getUserId($_POST["myusername1"]);
                                    $fname = getFileName($f);
                                    $v = getGlobalFileRights($f);

                                    if ($v != null) {
                                        if (array_key_exists($user_id, $v)) {
                                            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Files/Permissions<br><br>";
                                            $rights = getFileRights($f, $user_id);
                                            echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . "<a href = " . '"file_edit.php?uid=' . $_POST["myusername1"] . '&id_f=' . $f . '">' . $fname . "</a>&nbsp;&nbsp;" . $rights . "<br>";
                                        }
                                    }
                                }
                            }
                            ?>
                        </div>
                    </div>
                    <div class="post">
                        <h2><a href="#">Create New File</a></h2>
                        <form name="form2" method="post" action="file_edit.php">
                            <input name="file" type="text" id="file"/>
                            <input type="submit" name="Submit" value="Create"/>
                            <input type="hidden" name="username"
                            <?php
                            echo 'value = "' . $_POST["myusername1"] . '"';
                            ?>
                                   />
                        </form>

                    </div>
                </div><!-- content -->
                <div class="clearing">&nbsp;</div>
            </div></div><!-- main --><!-- main2 -->
        <div id="footer"><div id="footer2">
                <p>Copyright &copy; 2011, designed by <a href="http://www.webtemplateocean.com/">WebTemplateOcean.com</a></p>
            </div></div>
        <div style="text-align: center; font-size: 0.75em;">Design downloaded from <a href="http://www.freewebtemplates.com/">free website templates</a>.</div></body>
</html>
